<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $configs['title']; ?></title>
</head>
<link href="css/tool.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src='js/jquery-1.2.6.min.js'></script>
<body>
	<form name="urlform" action="" method="POST">
	<br />
	<table width="400" align="left" cellpadding="0" cellspacing="0" id="myTable">
	<!--主表选择-->
		<tr>
			<th colspan="4"><?php echo $dbId;?>数据库 主表选择 [<a href="index.php?act=logout">退出</a>]</td>
		</tr>
		<tr>
			<td colspan="4">&nbsp;&nbsp;<select id='mainTab'>
			<option value="0">请选择主表</option>
			<?php
			    foreach ($data as $key => $value){
			    	echo "<option value={$value}>",$value,'</option>';
			    }
			?>			
			</select></td>
		</tr>
		<tr>
			<th colspan="4">&nbsp;&nbsp;<input name="compare_ta" type="button" id="compare_db" value="对比所选中的表" onclick="compareTabs('<?php echo $dbId;?>');"> <input  type="button"  value="返回" onclick="window.location.href='comparedb.php';"> 
				<input type="button"  value="固定表比较" onclick="showDiv('divtab');"> <input type="button"  value="删除" onclick="if (confirm('确定删除吗?')) backTable('<?php echo $dbId;?>', 'deltable', 'dbids');">
			</th>
		</tr>
		<!--表比较-->
		<tr>
			<th colspan="4"><input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox'> 全选&nbsp;&nbsp;副表选择 &nbsp;&nbsp;<input name="show_unkey" type="button" id="show_unkey" value="查看unkey" onclick="showDiv('unkey');"></td>
		</tr>
		<?php
			foreach ($data as $key => $value){
				if (is_int($key/2)) {
					echo '<tr>';
				}
		?>
			<td align="center"><input type='checkbox' name='dbids' value=<?php echo $value;?> /></td><td><?php echo $value;?> <?php if(@in_array($value, $mmc)){echo '(<font color=red>缓存</font>)';} ?></td>
		<?php
				if (!is_int($key/2)) {
					echo '</tr>';
				}			
			}
		?>
		<tr>
			<th colspan="4">&nbsp;&nbsp;<input name="compare_ta" type="button" id="compare_db" value="对比所选中的表" onclick="compareTabs('<?php echo $dbId;?>');"> <input  type="button"  value="返回" onclick="window.location.href='comparedb.php';">
				<input type="button"  value="固定表比较" onclick="$('#divtab').show();"> <input type="button"  value="删除" onclick="if (confirm('确定删除吗?')) backTable('<?php echo $dbId;?>', 'deltable', 'dbids');"></th>
		</tr>
	</form>	
	<form>	
	<!--备份表-->	
		<tr>
			<th colspan="4"><input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox'> 全选&nbsp;&nbsp;还原操作</td>
		</tr>
		<?php
		foreach ($back as $key => $value){
			if (is_int($key/2)) {
				echo '<tr>';
			}
		?>
			<td align="center"><input type='checkbox' name='backids' value=<?php echo $value;?> /></td><td><?php echo $value;?></td>
		<?php
		if (!is_int($key/2)) {
			echo '</tr>';
		}
		}
		?>
		<tr>
			<th colspan="4">&nbsp;&nbsp;<input name="back_tabs" type="button" id="back_tabs" value="还原" onclick="if (confirm('确定还原吗?')) backTable('<?php echo $dbId;?>', 'backtable', 'backids');"> <input  type="button"  value="返回" onclick="window.location.href='comparedb.php';">
				<input type="button"  value="删除" onclick="if (confirm('确定删除吗?')) backTable('<?php echo $dbId;?>', 'deltable', 'backids');"></th>
		</tr>
	</table>
	</form>
	<!--固定表比较-->
	<div id="divtab" style="display:none;">	
		<form name="urlform" action="" method="POST">
		<table cellpadding="0" cellspacing="0" >
			<tr>
				<th colspan="2"><input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox'> 全选 固定表比较 <a href='#' onclick="compareSpe('<?php echo $dbId;?>')">对比选中项</a></th>
			</tr>
			<?php
			if (!$configs['type']) {
				echo '<tr><td>没有设置</td></tr>';
			}else{
				foreach ($configs['type'] as $key => $value){
			?>
				<tr>
					<td><input type='checkbox' name='tdbids' value=<?php echo $key;?> /></td><td><a href="#" onclick="comparePartTabs('<?php echo $dbId,'\',\'',$value[0],'\',\'',$value[1];?>')"><?php echo $key;?></a></td>
				</tr>
			<?php
				}
			}
			?>
		</table>	
		</form>
	</div>
	
	<div id="viewtab" align="center"></div><div id="loaddiv" align="center" style="display:none;"><img src="images/loading.gif">正在扫描数据库...</div>	
	
	<div id="unkey" style="display:none;">
	<table cellpadding="0" cellspacing="0" id="djb">
		<tr><th>表名</th><th>unkey</th></tr>
		<?php 
		foreach ($mmc as $key => $value){
			if (isset($g_mmc_tbconf[$value]['unkey'])) {
				echo '<tr><td>'.$value.'</td><td>'.implode(', ', $g_mmc_tbconf[$value]['unkey']).'</td></tr>';
			}else {
				echo '<tr><td>'.$value.'</td><td><font color=red>未设定</font></td></tr>';
			}
		}
		?>
		</table>
	</div>
	
<script type="text/javascript">
//查看
function showDiv(id){
	if($('#'+id).is(":hidden")) $('#'+id).show();
	else $('#'+id).hide();
}

//比较表
function compareTabs(dbId){
	if ($('#mainTab').val() == 0){
		alert('请选择主表!');
		return;
	}

	var str="";
	$("[name='dbids'][checked]").each(function(){
		str+=$(this).val()+"|";
	})
	$.ajax({
		type:'POST',
		async:false,
		url:'ajax/comparedb.ajax.php',
		data:"act=comparetable&dbId="+dbId+"&tabId="+str+"&mainTab="+$('#mainTab').val(),
		beforeSend:function () {
			$('#loaddiv').show();
		},
		success:function(msg){
			$('#divtab').hide();
			$('#loaddiv').hide();
			$('#viewtab').html(msg);
		}
	})
}
//比较特定表集合
function comparePartTabs(dbId, main, str){
	$.ajax({
		type:'POST',
		async:false,
		url:'ajax/comparedb.ajax.php',
		data:"act=comparetable&dbId="+dbId+"&tabId="+str+"&mainTab="+main,
		success:function(msg){
			$('#divtab').hide();
			$('#viewtab').html(msg);
		}
	})
}
//比较多个特定表集合
function compareSpe(dbId){
	var str="";
	$("[name='tdbids'][checked]").each(function(){
		str+=$(this).val()+"|";
	})
	$.ajax({
		type:'POST',
		async:false,
		url:'ajax/comparedb.ajax.php',
		data:"act=comparespe&dbId="+dbId+"&tabId="+str,
		beforeSend:function () {
			$('#loaddiv').show();
		},
		success:function(msg){
			$('#divtab').hide();
			$('#loaddiv').hide();
			$('#viewtab').html(msg);
		}
	})
}
//还原删除
function backTable(dbId, type, id){
	var t;
	if (type == 'deltable'){
		t = prompt('请输入密钥');
	}
	
	var str="";
	$("[name='"+id+"'][checked]").each(function(){
		str+=$(this).val()+"|";
	})
	$.ajax({
		type:'POST',
		async:false,
		url:'ajax/comparedb.ajax.php',
		data:"act="+type+"&dbId="+dbId+"&tabId="+str+"&key="+t,
		beforeSend:function () {
			$('#loaddiv').show();
		},
		success:function(msg){
			$('#loaddiv').hide();
			alert(msg);
			window.location.reload();  
		}
	})
}

//同步
function asynTable(type, tab, main, db){
	$.ajax({
		type:'POST',
		async:false,
		url:'ajax/comparedb.ajax.php',
		data:"act="+type+"&tabId="+tab+"&mainTab="+main+"&dbId="+db+"&is_back="+$('#is_back[checked]').val(),
		beforeSend:function () {
			$('#loaddiv').show();
		},
		success:function(msg){
			$('#loaddiv').hide();
			alert(msg);
		}
	})
}
//全选
function unselectall(){
	if(document.myform.chkAll.checked){
		document.myform.chkAll.checked = document.myform.chkAll.checked&0;
	}
}
function CheckAll(form){
	for (var i=0;i<form.elements.length;i++){
		var e = form.elements[i];
		if (e.Name != 'chkAll'&&e.disabled==false)
		e.checked = form.chkAll.checked;
	}
}
</script>
</body>